var AREA = '';
$(function () {
    // // 获取默认地址
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var result = Model.user.getUserInfo();
    if (result.loginedUser.address == null) {
        $('#showTooltips').removeClass('update-btn');
        loading.hide();
        return false;
    } else {
        $('#showTooltips').removeClass('save-btn');
        loading.hide();
    }
    $('#showPicker').html(result.loginedUser.address.area);
    $('#input-name').val(result.loginedUser.address.receiver);
    $('#input-phone').val(result.loginedUser.address.phone);
    $('#input-address').val(result.loginedUser.address.address);
    $('#input-code').val(result.loginedUser.address.postalcode);
    loading.hide();
});

// 提交信息按钮
$(document).on('click', '.update-btn', function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var info = {
        name: $.trim($('#input-name').val()),
        phone: $.trim($('#input-phone').val()),
        address: $.trim($('#input-address').val()),
        code: $.trim($('#input-code').val()),
        area: $.trim($('#showPicker').html())
    };
    for (i in info) {
        if (info[i] == '') {
            weui.alert('信息不得为空');
            return false;
        }
    }
    var result = Model.user.modelifyAddress({
        receiver: info.name,
        phone: info.phone,
        area: info.area,
        address: info.address,
        postalcode: info.code
    });
    if (result.code == 1) {
        loading.hide();
        weui.toast('修改成功', {
            duration: 1500,
            className: 'custom-classname',
            callback: function () {
                location.reload();
            }
        });

    } else {
        loading.hide();
        weui.toast('修改失败，系统错误', {
            duration: 1500,
            className: 'custom-classname',
            callback: function () {
                // location.reload()
            }
        });
    }
});


// 提交信息按钮
$(document).on('click', '.save-btn', function () {
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var info = {
        name: $.trim($('#input-name').val()),
        phone: $.trim($('#input-phone').val()),
        address: $.trim($('#input-address').val()),
        code: $.trim($('#input-code').val()),
        area: $.trim($('#showPicker').html())
    };
    for (i in info) {
        if (info[i] == '') {
            weui.alert('信息不得为空');
            return false;
        }
    }
    var result = Model.user.addAddress({
        receiver: info.name,
        phone: info.phone,
        area: info.area,
        address: info.address,
        postalcode: info.code
    });
    if (result.code == 1) {
        loading.hide();
        weui.toast('添加地址成功', {
            duration: 1500,
            className: 'custom-classname',
            callback: function () {
                location.reload();
            }
        });

    } else {
        loading.hide();
        weui.toast('添加地址失败，系统错误', {
            duration: 1500,
            className: 'custom-classname',
            callback: function () {
                // location.reload()
            }
        });
    }
});

// 选择地区下拉菜单
$(document).on('click', '#showPicker', function () {
    weui.picker(Config.province, {
        className: 'custom-classname',
        container: 'body',
        defaultValue: [AREA],
        onChange: function (result) {
            console.log(result)
        },
        onConfirm: function (result) {
            $('#showPicker').html(result);
        },
        id: 'singleLinePicker'
    });
});